import java.io.*;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
public class FileOperator {
    public static void main(String[] args) {
        System.out.println(selectFile("",JFileChooser.SAVE_DIALOG));
    }
 
    //打开文件对话框，返回已选择的文件名
    public static String selectFile(String pathFileName,int dialogType) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch(Exception e) {
            //...
        }
        // 创建文件选择器
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);//只允许选择文件
        fileChooser.setMultiSelectionEnabled(false);//不允许多选
        // 设置当前目录
        if (!pathFileName.equals(""))
            fileChooser.setCurrentDirectory(new File(pathFileName));
        fileChooser.setAcceptAllFileFilterUsed(false);//先取消文件过滤器
 
        final String[][] fileExtNames = {
                {".cvs", "CVS文件(*.cvs)"},
                {".txt", "文本文档(*.txt)"}
        };
 
        // 显示所有文件
        fileChooser.addChoosableFileFilter(new FileFilter() {
            public boolean accept(File file) {
                return true;
            }
 
            public String getDescription() {
                return "所有文件(*.*)";
            }
        });
 
        // 循环添加需要显示的文件
        for (final String[] fileExtName : fileExtNames) {
            fileChooser.setFileFilter(new FileFilter() {
                public boolean accept(File file) {
                    if (file.getName().toLowerCase().endsWith(fileExtName[0]) || file.isDirectory()) {
                        return true;
                    }
                    return false;
                }
 
                public String getDescription() {
                    return fileExtName[1];
                }
            });
        }
 
        fileChooser.setDialogType(dialogType);
 
        if (fileChooser.showDialog(null,null) ==JFileChooser.APPROVE_OPTION) {
            return fileChooser.getSelectedFile().getPath();
        } else {
            return "";//没有选择文件，比如取消了
        }
    }
 
 
    public static String readTxt(String path) {
        StringBuilder content = new StringBuilder("");
        try {
            String fileCharsetName = getFileCharsetName(path);
            //System.out.println("文件的编码格式为："+fileCharsetName);
 
            InputStream is = new FileInputStream(path);
            //必须保证此处文件编码判断无误
            InputStreamReader isr = new InputStreamReader(is, fileCharsetName);
            BufferedReader br = new BufferedReader(isr);
            BufferedReader br2 = new BufferedReader(isr);
            String str = "";
            boolean isFirst = true;
            long start2 = System.currentTimeMillis();
            while (null != (str = br.readLine())) {
                if (!isFirst)
                    content.append(System.lineSeparator());
                else
                    isFirst = false;
                content.append(str);
            }
            long end2 = System.currentTimeMillis();
            System.out.println("charArray:"+(end2-start2));
 
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("读取文件:" + path + "失败!");
        }
        return content.toString();
    }
 
    public static boolean writeTxt(String path,String fileContent,String fileCharsetName) {
        boolean saveOK = false;
        try {
 
            OutputStream os = new FileOutputStream(path);
            //必须保证此处文件编码判断无误
            OutputStreamWriter osr = new OutputStreamWriter(os, fileCharsetName);
            BufferedWriter wr = new BufferedWriter(osr);
 
            wr.write(fileContent);
            wr.close();
            saveOK = true;
        } catch (Exception e) {
            System.err.println("文件存盘失败:" + path + "失败!"+e.getMessage());
        }
        return saveOK;
    }
 
    public static String getFileCharsetName(String fileName) throws IOException {
        String code = "UTF-8";
        try {
            code = EncodeUtils.getEncode(fileName, true);
            System.out.println(code);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return code;
    }
}